package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobIdentityInfo;
import com.ffcs.crmd.tsp.server.jdbc.entity.TaskIdentity;
import com.ffcs.crmd.tsp.server.jdbc.repository.TaskIdentityRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.IJobIdentityInfoService;
import com.ffcs.crmd.tsp.server.jdbc.service.ITaskIdentityService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class TaskIdentityServiceImpl implements ITaskIdentityService {
    
    private IJobIdentityInfoService jobIdentityInfoService;
    
    private TaskIdentityRepository  taskIdentityRepository = new TaskIdentityRepository();
    
    public TaskIdentityServiceImpl() {
    }
    
    @Override
    public TaskIdentity get(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(taskIdentity.getClusterName());
        params.add(taskIdentity.getIdentity());
        params.add(taskIdentity.getId());
        return taskIdentityRepository.query(GET_TASK_IDENTITY, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_TASK_IDENTITY, taskIdentity, params, true);
        return taskIdentityRepository.queryRows(sql, params, conn);
    }
    
    @Override
    public List<TaskIdentity> query(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_TASK_IDENTITY, taskIdentity, params, false);
        return taskIdentityRepository.queryList(sql, params, conn);
    }
    
    @Override
    public List<TaskIdentity> queryPage(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_TASK_IDENTITY, taskIdentity, params);
        return taskIdentityRepository.queryList(sql, params, conn);
    }
    
    @Override
    public boolean add(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(taskIdentity.getIdentity());
        params.add(taskIdentity.getName());
        params.add(taskIdentity.getDescription());
        params.add(taskIdentity.getThreadNum());
        params.add(taskIdentity.getThreadPriorityWeight());
        params.add(taskIdentity.getBatchThreadNum());
        params.add(taskIdentity.getBatchThreadPriorityWeight());
        params.add(taskIdentity.getJobGroupId());
        params.add(taskIdentity.getClusterName());
        return taskIdentityRepository.execute(INSERT_TASK_IDENTITY, params, conn);
    }
    
    @Override
    public boolean delete(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpDelete(DELETE_TASK_IDENTITY, taskIdentity, params);
        
        List<TaskIdentity> taskIdentityList = new ArrayList<TaskIdentity>(); 
        TaskIdentity qtaskIdentity = this.get(conn, taskIdentity);
        if(qtaskIdentity != null){
            taskIdentityList.add(qtaskIdentity);
        }else{
            taskIdentityList.addAll(this.query(conn, taskIdentity));
        }
        
        for (TaskIdentity rtaskIdentity : taskIdentityList) {
            JobIdentityInfo jobIdentityInfo = new JobIdentityInfo();
            jobIdentityInfo.setTaskIdentityId(rtaskIdentity.getId());
            jobIdentityInfoService.delete(conn, jobIdentityInfo);
        }
        return taskIdentityRepository.execute(sql, params, conn);
    }
    
    @Override
    public boolean update(Connection conn, TaskIdentity taskIdentity) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(taskIdentity.getIdentity());
        params.add(taskIdentity.getName());
        params.add(taskIdentity.getDescription());
        params.add(taskIdentity.getThreadNum());
        params.add(taskIdentity.getThreadPriorityWeight());
        params.add(taskIdentity.getBatchThreadNum());
        params.add(taskIdentity.getBatchThreadPriorityWeight());
        params.add(taskIdentity.getJobGroupId());
        params.add(taskIdentity.getId());
        
        JobIdentityInfo jobIdentityInfoParam = new JobIdentityInfo();
        jobIdentityInfoParam.setTaskIdentityId(taskIdentity.getId());
        
        
        List<JobIdentityInfo> jobIdentityInfoList = jobIdentityInfoService.query(conn, jobIdentityInfoParam);
        for(JobIdentityInfo jobIdentityInfo : jobIdentityInfoList){
            jobIdentityInfo.setIdentity(taskIdentity.getIdentity());
            jobIdentityInfoService.update(conn, jobIdentityInfo);
        }
        
        return taskIdentityRepository.execute(UPDATE_TASK_IDENTITY, params, conn);
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param taskIdentity
     * @param params
     * @return
     */
    private String rtp(String sql, TaskIdentity taskIdentity, List<Object> params, boolean isLike) {
        if (taskIdentity.getJobGroupId() != null) {
            sql += " AND " + COL_TASK_IDENTITY_JOB_GROUP_ID + " = ?";
            params.add(taskIdentity.getJobGroupId());
        }
        
        
        if (StringUtils.isNoneBlank(taskIdentity.getIdentity())) {
            if (isLike) {
                sql += " AND " + COL_TASK_IDENTITY_IDENTITY + " LIKE ?";
                params.add("%" + taskIdentity.getIdentity() + "%");
            }else{
                sql += " AND " + COL_TASK_IDENTITY_IDENTITY + " = ?";
                params.add(taskIdentity.getIdentity());
            }
        }
        
        if (StringUtils.isNoneBlank(taskIdentity.getName())) {
            sql += " AND " + COL_NAME + " LIKE ?";
            params.add("%" + taskIdentity.getName() + "%");
        }
        
        if (StringUtils.isNoneBlank(taskIdentity.getClusterName())) {
            sql += " AND " + COL_CLUSTER_NAME + " = ?";
            params.add(taskIdentity.getClusterName());
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param taskIdentity
     * @param params
     * @return
     */
    private String rtpPage(String sql, TaskIdentity taskIdentity, final List<Object> params) {
        String rsql = rtp(sql, taskIdentity, params, true);
        rsql += " LIMIT ?,?";
        params.add(taskIdentity.getPageNumber());
        params.add(taskIdentity.getPageSize());
        return rsql;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param sliceGroup
     * @param params
     * @return
     * @throws TspException 
     */
    private String rtpDelete(String sql, TaskIdentity taskIdentity, final List<Object> params) throws TspException {
        if (taskIdentity.getJobGroupId() == null && taskIdentity.getId() == null) {
            throw new TspException("['jobGroupId' or 'id'] can't be null at the same time");
        }
        
        if (taskIdentity.getId() != null) {
            sql += " AND " + COL_ID + " = ?";
            params.add(taskIdentity.getId());
        }
        
        if (taskIdentity.getJobGroupId() != null) {
            sql += " AND " + COL_TASK_IDENTITY_JOB_GROUP_ID + " = ?";
            params.add(taskIdentity.getJobGroupId());
        }
        
        return sql;
    }
    
    
    /**
     * 功能说明: 设置jobIdentityInfoService
     *
     * @param jobIdentityInfoService jobIdentityInfoService 
     */
    @Override
    public void setJobIdentityInfoService(IJobIdentityInfoService jobIdentityInfoService) {
        this.jobIdentityInfoService = jobIdentityInfoService;
    }
    
}
